﻿using PokemonBattle.BattleNetwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PBO.Game.BattleClass
{
    public partial class Battle
    {
        public BattleGroup Group1 { get; private set; }
        public BattleGroup Group2 { get; private set; }

        private void InitializeGroup()
        {
            if (this.Mode == BattleMode.Double_4P)
            {
                this.Group1.AddTeam(1, _players[1], _teamDatas[1]);
                this.Group1.AddTeam(2, _players[2], _teamDatas[2]);
                this.Group2.AddTeam(3, _players[3], _teamDatas[3]);
                this.Group2.AddTeam(4, _players[4], _teamDatas[4]);
            }
            else
            {
                this.Group1.AddTeam(1, _players[1], _teamDatas[1]);
                this.Group2.AddTeam(2, _players[2], _teamDatas[2]);
            }
            this.Group1.Opponent = this.Group2;
            this.Group2.Opponent = this.Group1;
        }

        private static void BeginTurn(IEnumerable<Pokemon> pokemons)
        {
            foreach (Pokemon pm in pokemons)
            {
                pm.BattleState.moved = false;
                //pm.BattleState.swaped = false;
                pm.turnHurt.Clear();
                pm.turnHurtMove.Clear();
                pm.turnHurtBy.Clear();
                pm.attackSuccessed = false;
                if (pm.BattleState.stayOneTurn)
                    pm.BattleState.firstTurnAtk = false;
                pm.BattleState.stayOneTurn = true;
                pm.CheckTrait();
            }
        }

        private static BattleTeam GetTeamByIndex(Battle battle, int index)
        {
            return battle.Teams.First(t => t.Id == index);
        }

        public PokemonIndex GetPokemonIndex(Pokemon pm)
        {
            int i = pm.MyTeam.Group.SelectedPokemon.ToList().IndexOf(pm);
            if (pm.MyTeam.Group == this.Group1)
            {
                return i == 0 ? PokemonIndex.Pokemon1OfTeam1 : PokemonIndex.Pokemon2OfTeam1;
            }
            else
            {
                return i == 0 ? PokemonIndex.Pokemon1OfTeam2 : PokemonIndex.Pokemon2OfTeam2;
            }
        }

        private BattleGroup GetGroupFromIndex(PokemonIndex index)
        {
            switch (index)
            {
                case PokemonIndex.Pokemon1OfTeam1:
                case PokemonIndex.Pokemon2OfTeam1:
                    return this.Group1;
                case PokemonIndex.Pokemon1OfTeam2:
                case PokemonIndex.Pokemon2OfTeam2:
                    return this.Group2;
                default:
                    return null;
            }
        }

        public Pokemon GetPokemonFromIndex(PokemonIndex index)
        {
            var g = GetGroupFromIndex(index);
            switch (index)
            {
                case PokemonIndex.Pokemon1OfTeam1:
                case PokemonIndex.Pokemon1OfTeam2:
                    return g.SelectedPokemon.ElementAtOrDefault(0);
                case PokemonIndex.Pokemon2OfTeam1:
                case PokemonIndex.Pokemon2OfTeam2:
                    return g.SelectedPokemon.ElementAtOrDefault(1);
                default:
                    return null;
            }
        }

        public IEnumerable<Pokemon> SelectedPokemons
        {
            get { return Group1.SelectedPokemon.Union(Group2.SelectedPokemon); }
        }

        public IEnumerable<BattleTeam> Teams
        {
            get { return Group1.Teams.Union(Group2.Teams); }
        }
    }
}
